<?php

/**
 * @application Automatical Judging System
 * @author deltamaster
 * @copyright Softrank Research Center
 * @version 2.1.0.0
 */

require_once "./include/common.inc.php";

$currenttime = time();
if ($uid && $started && !$ended || $adminid == 1)
{
	if (submitcheck('submitanswer'))
	{
		if (empty($pid))
			showmessage($lang['invalid_operation'], 'index.php'); //Without a pid
		$query = $db->query("SELECT * FROM `{$tablepre}problem` WHERE `id`='{$pid}'");
		if (!$db->num_rows($query))
			showmessage($lang['invalid_operation'], 'index.php'); //pid not found
		$problem = $db->fetch_array($query);
		$problem['problem'] = unserialize(base64_decode($problem['problem']));
		$query = $db->query("DELETE FROM `{$tablepre}record` WHERE `type`='next' AND `uid`='{$uid}'");
		if (is_array($inputanswer)) $inputanswer = array_sum($inputanswer);
		if (($problem['type'] == 1 && $problem['manual'] == 0) || $problem['type'] == 2 || $problem['type'] == 3)
		{
			if (preg_match('/'.$problem['problem']['judge'].'/', $inputanswer)) $event = $problem['score'];
			else $event = 0;
			if ($db->query("INSERT INTO `{$tablepre}record` (`uid`, `pid`, `type`, `event`) VALUES ('{$uid}', '{$pid}', 'judge', '{$event}')"))
				showmessage($lang['submit_success'].$lang['judge_auto'], 'question.php');
			else
				showmessage($lang['submit_fail'], 'question.php');
			
		}
		else
		{
			if ($db->query("INSERT INTO `{$tablepre}record` (`uid`, `pid`, `type`, `event`) VALUES ('{$uid}', '{$pid}', 'submit', '{$inputanswer}')"))
				showmessage($lang['submit_success'].$lang['judge_manual'], 'question.php');
			else
				showmessage($lang['submit_fail'], 'question.php');
		}	
		
	}
	
	$query = $db->query("SELECT * FROM `{$tablepre}record` WHERE `type`='next' AND `uid`='{$uid}'");
	$reselect = false;
	if ($db->num_rows($query))
	{
		$record = $db->fetch_array($query);
		//echo "next problem record exists.";
		$query = $db->query("SELECT * FROM `{$tablepre}problem` WHERE `id`='{$record['pid']}'");
		if (!$db->num_rows($query))
		{
			$db->query("DELETE FROM `{$tablepre}record` WHERE `type`='next' AND `uid`='{$uid}'");
			$reselect = true;
			$query = $db->query("SELECT * FROM `{$tablepre}problem` AS t1 JOIN (SELECT ROUND(RAND() * ((SELECT MAX(id) FROM `{$tablepre}problem`)-(SELECT MIN(id) FROM `{$tablepre}problem`))+(SELECT MIN(id) FROM `{$tablepre}problem`)) AS tid) AS t2 WHERE t1.id >= t2.tid AND NOT EXISTS (SELECT * FROM `{$tablepre}record`,`sj_problem` WHERE (`{$tablepre}record`.`type`='submit' OR `{$tablepre}record`.`type`='judge') AND `{$tablepre}record`.`uid`='{$uid}' AND t1.`id`=`{$tablepre}record`.`pid`) ORDER BY t1.id LIMIT 1");
		}
	}
	else
	{
		$reselect = true;
		$query = $db->query("SELECT * FROM `{$tablepre}problem` AS t1 JOIN (SELECT ROUND(RAND() * ((SELECT MAX(id) FROM `{$tablepre}problem`)-(SELECT MIN(id) FROM `{$tablepre}problem`))+(SELECT MIN(id) FROM `{$tablepre}problem`)) AS tid) AS t2 WHERE t1.id >= t2.tid AND NOT EXISTS (SELECT * FROM `{$tablepre}record`,`sj_problem` WHERE (`{$tablepre}record`.`type`='submit' OR `{$tablepre}record`.`type`='judge') AND `{$tablepre}record`.`uid`='{$uid}' AND t1.`id`=`{$tablepre}record`.`pid`) ORDER BY t1.id LIMIT 1");
	}
	$num = $db->num_rows($query);
	if ($num)
	{
		$p = $db->fetch_array($query);
		$p['problem'] = unserialize(base64_decode($p['problem']));
		//print_r($p);
		if ($reselect) $db->query("INSERT INTO `{$tablepre}record` (`uid`, `pid`, `type`, `event`) VALUES ('{$uid}', '{$p['id']}', 'next', '')");
	}
	else
	{
		showmessage($lang['no_more_question']);
	}
	
	
}

include template('question');

?>